﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBService;
using DBService.ViewDatas;


public partial class SearchResult : System.Web.UI.Page
{
    int PageSize = 10;
    int ItemCount;
    int PagesCount;


    protected enum SortBy
    {
        NONE,
        HIGHRATING,
        MOSTREVIEWED
    }

    private SortBy SortID;

    ModelAdapter ma = new ModelAdapter();
    List<ListSearchCompanies_Result> filteredCompanies;


    protected void Page_Load(object sender, EventArgs e)
    {
        SearchText.Attributes.Add("onkeypress", "return clickButton(event,'" + SearchBigButton.ClientID + "')");
        Master.FindControl("bg_search").Visible = false;

        if (Session["keyword"] != null && !string.IsNullOrEmpty(Session["keyword"].ToString()))
        {
            SearchText.Text = Session["keyword"].ToString();
            Session["keyword"] = "";
        }
        if (!IsPostBack)
        {
            BindRepeater();
        }
    }

    private List<ListSearchCompanies_Result> SortList(List<ListSearchCompanies_Result> filteredCompanies)
    {
        switch (SortID)
        {
            case SortBy.HIGHRATING:
                filteredCompanies = filteredCompanies.OrderByDescending(c => c.Rating).ToList();
                HighRatingSpan.Attributes.CssStyle.Clear();
                HighRatingSpan.Attributes.Add("class", "act_cheked");

                MostReviewedSpan.Attributes.CssStyle.Clear();
                MostReviewedSpan.Attributes.Add("class", "uncheked");
                return filteredCompanies;
            //break;
            case SortBy.MOSTREVIEWED:
                filteredCompanies = filteredCompanies.OrderByDescending(c => c.ReviewsCount).ToList();
                MostReviewedSpan.Attributes.CssStyle.Clear();
                MostReviewedSpan.Attributes.Add("class", "act_cheked");

                HighRatingSpan.Attributes.CssStyle.Clear();
                HighRatingSpan.Attributes.Add("class", "uncheked");
                return filteredCompanies;
            //break;
            default:
                //break;
                return filteredCompanies;
        }
    }


    protected void SearchBigButton_Click(object sender, EventArgs e)
    {
        filteredCompanies = ma.GetSearchedCompanies(SearchText.Text);
        ViewState["K"] = "";
        SortID = SortBy.NONE;

        MostReviewedSpan.Attributes.CssStyle.Clear();
        MostReviewedSpan.Attributes.Add("class", "uncheked");

        HighRatingSpan.Attributes.CssStyle.Clear();
        HighRatingSpan.Attributes.Add("class", "uncheked");

        BindRepeater();
    }



    protected void Positive100_Click(object sender, ImageClickEventArgs e)
    {
        ViewState["K"] = 4.85;
        BindRepeater();
    }
    protected void Positive90_Click(object sender, ImageClickEventArgs e)
    {
        ViewState["K"] = 4.5;
        BindRepeater();
    }
    protected void Positive70_Click(object sender, ImageClickEventArgs e)
    {
        ViewState["K"] = 3.5;
        BindRepeater();
    }
    protected void Positive50_Click(object sender, ImageClickEventArgs e)
    {
        ViewState["K"] = 2.5;
        BindRepeater();
    }
    protected void ShowAllPositive_Click(object sender, EventArgs e)
    {
        ViewState["K"] = -1;
        BindRepeater();
    }

    protected void MostReviewed_Click(object sender, EventArgs e)
    {
        SortID = SortBy.MOSTREVIEWED;
        BindRepeater();
    }
    protected void HighRating_Click(object sender, EventArgs e)
    {
        SortID = SortBy.HIGHRATING;
        BindRepeater();
    }

    private void BindRepeater()
    {
        filteredCompanies = ma.GetSearchedCompanies(SearchText.Text);

        double R;
        if (ViewState["K"] == null || !double.TryParse(ViewState["K"].ToString(), out R))
        {
            R = -1;
        }
        filteredCompanies = filteredCompanies.Where(c => (c.Rating.HasValue && c.Rating.Value > R) || R == -1).ToList();
        if (filteredCompanies.Count == 0)
        {
            NoSuchCompanies.Text = "ასეთი კომპანია არ მოიძებნა, თქვენ შეგიძლიათ <a href='newcompany.aspx' style='color:Blue;'>დაამატოთ</a> ახლავე";
        }
        else
        {
            filteredCompanies = SortList(filteredCompanies);
        }


        //pages.Controls.Clear();
        //ItemCount = filteredCompanies.Count;

        //PagedDataSource Pds1 = new PagedDataSource();
        //Pds1.DataSource = filteredCompanies;
        //Pds1.AllowPaging = true;
        //Pds1.PageSize = PageSize;
        //SearchResultCompanies.DataSource = Pds1;
        SearchResultCompanies.DataSource = filteredCompanies;


        //var lastIndex = PagesCount = ItemCount % PageSize == 0 ? (ItemCount / PageSize) : (ItemCount / PageSize) + 1;
        //var startIndex = 1;

        //if (lastIndex - startIndex > 0)
        //{
        //    Pds1.CurrentPageIndex = CurrentPage;
        //    for (int i = startIndex; i <= lastIndex; i++)
        //    {
        //        var lb = new LinkButton() { Text = i.ToString(), CssClass = "page_bg_num" };
        //        lb.ID = "btnlink" + i.ToString();
        //        lb.CssClass = "page_bg_num";
        //        lb.Click += new EventHandler(lb_Click);
        //        pages.Controls.Add(lb);
        //    }
        //    if (CurrentPage != lastIndex - 1)
        //    {
        //        // show next
        //        var lb1 = new LinkButton() { Text = "►", CssClass = "page_bg_num" };
        //        lb1.ID = "btnlink-1";
        //        lb1.Click += new EventHandler(lb_Click);
        //        pages.Controls.Add(lb1);

        //        // show last
        //        var lblast = new LinkButton() { Text = "ბოლო", CssClass = "page_bg_num" };
        //        lblast.ID = "lblast100";
        //        lblast.CssClass = "page_last";
        //        lblast.Click += new EventHandler(lb_Click);
        //        pages.Controls.Add(lblast);
        //    }
        //}
        SearchResultCompanies.DataBind();
    }

    protected void SearchResultCompanies_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        var item = (ListSearchCompanies_Result)e.Item.DataItem;
        if (item != null)
        {
            var l = (Literal)e.Item.FindControl("LitRatCalc");
            l.Text = "<div class=\"inner-star\" style=\"width: " + ((int)((item.Rating ?? 0) * 20)).ToString() + "%; margin-left: 1px;\"></div>";
        }
    }

    /* გვერდების გადანომრვა. *
    protected void lb_Click(object sender, EventArgs e)
    {
        var lb = (LinkButton)sender;
        if (lb.Text == "ბოლო")
        {
            CurrentPage = 10000;
        }
        else if (lb.Text == "►")
        {
            CurrentPage += 1;
        }
        else
        {
            CurrentPage = int.Parse(lb.Text) - 1;
        }
        BindRepeater();
    }

    public int CurrentPage
    {
        get
        {
            object s1 = this.ViewState["CurrentPage"];
            if (s1 == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(s1);
            }
        }
        set
        {
            if (value > PagesCount - 1)
            {
                value = PagesCount - 1;
            }
            this.ViewState["CurrentPage"] = value;
        }
    }
    */
}